//
//  LESegmentView.m
//  CreditAddressBook
//
//  Created by Lee on 15/10/12.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "LESegmentView.h"

/**
 *  让类方法中的对象被持有
 */
static ClickAtIndexBlock _clickAtIndexBlock;
static CGFloat _minusLineW = 20;//导航条减去的宽度

@interface LESegmentView ()<XCTabBarDelegate>

@property (nonatomic , assign) CGFloat segmentBottomLineX;//segmentBottomLine的X值，适配ios7
@property (nonatomic , assign) NSInteger fromIndex;//导航条下标缓存
@property (nonatomic ,strong) UIView *indexLine;//
@property (nonatomic ,strong) UIView *bottomLine;//

@property (nonatomic ,strong) XCTabBarView *segmentView;
@end

@implementation LESegmentView

- (void)setSegmentSelectedIndex:(LESelectedIndex)segmentSelectedIndex {
    _segmentSelectedIndex = segmentSelectedIndex;
    
    if (self.segmentView) {
        self.segmentView.selectedIndex = segmentSelectedIndex;
    }
}

- (void)setIndexLineColor:(UIColor *)indexLineColor {
    _indexLineColor = indexLineColor;
    self.indexLine.backgroundColor = indexLineColor;
}

- (void)setBottomLineColor:(UIColor *)bottomLineColor {
    _bottomLineColor = bottomLineColor;
    self.bottomLine.backgroundColor = bottomLineColor;
}

+ (XCTabBarView *)segmentWithTitles:(NSArray *)titles andImageNames:(NSArray *)images andFrame:(CGRect)frame clickAtIndex:(ClickAtIndexBlock)clickAtIndex {
    XCTabBarView *tabBarView = [[XCTabBarView alloc] init];
    tabBarView.frame = frame;
    tabBarView.delegate = [self self];
    _clickAtIndexBlock = [clickAtIndex copy];
    
    if (titles && images) {
        for (int i = 0; i < titles.count; i++) {
            [tabBarView addTabBarButtonWithImageName:images[i] andSelImageName:nil andTitle:titles[i]];
        }
    }
    
    return tabBarView;
}

/**
 *  代理
 *
 *  @param tabBar
 *  @param from
 *  @param to
 */
- (void)tabBar:(XCTabBarView *)tabBar didSelectButtonFrom:(NSInteger)from to:(NSInteger)to {
    if (from != to) {
        _clickAtIndexBlock(to);
        [self segmentBottomLines:@[self.indexLine] to:to];
    }
}

//- (instancetype)initWithFrame:(CGRect)frame {
//    self = [super initWithFrame:frame];
//    if (self) {
//        
//    }
//    return self;
//}

#pragma mark - 添加按钮
- (void)addSegmentButtonWithTitles:(NSArray *)titles andImageNames:(NSArray *)images clickAtIndex:(ClickAtIndexBlock)clickAtIndex {
    _clickAtIndexBlock = [clickAtIndex copy];
    
    [self addSegmentSubviewWithCount:titles.count];
    
    if (titles && images) {
        for (int i = 0; i < titles.count; i++) {
            [self.segmentView addTabBarButtonWithImageName:images[i] andSelImageName:nil andTitle:titles[i]];
        }
    }
    [self addSubview:self.segmentView];
}

- (void)addSegmentButtonWithTitles:(NSArray *)titles clickAtIndex:(ClickAtIndexBlock)clickAtIndex {
    _clickAtIndexBlock = [clickAtIndex copy];
    
    [self addSegmentSubviewWithCount:titles.count];
    
    if (titles) {
        [self.segmentView addTabBarButtonWithTitles:titles andTitleColor:[UIColor blackColor] andselColor:CYNaviThemeColor andFont:[UIFont systemFontOfSize:14]];
    }
    [self addSubview:self.segmentView];
}

- (void)addSegmentSubviewWithCount:(NSInteger)count {
    XCTabBarView *tabBarView = [[XCTabBarView alloc] init];
    tabBarView.frame = self.bounds;
    tabBarView.height = self.height - 4;
    tabBarView.delegate = self;
    tabBarView.selectedIndex = (NSInteger)_segmentSelectedIndex;
    self.segmentView = tabBarView;
    
    UIView *indexView = [[UIView alloc] init];
    indexView.y = tabBarView.height;
    indexView.width = self.width;
    indexView.height = self.height - tabBarView.height - 1;
    indexView.backgroundColor = [UIColor clearColor];
    [self addSubview:indexView];
    
    UIView *indexLine = [[UIView alloc] init];
    self.indexLine = indexLine;
    indexLine.width = self.width/count - _minusLineW;
    indexLine.height = indexView.height;
    indexLine.x = _minusLineW/2.f;
    if (self.indexLineColor) {
        indexLine.backgroundColor = self.indexLineColor;
    } else {
        indexLine.backgroundColor = CYNaviThemeColor;
    }
    [indexView addSubview:indexLine];
    
    UIView *bottomLine = [[UIView alloc] init];
    self.bottomLine = bottomLine;
    bottomLine.width = self.width;
    bottomLine.height = 1;
    bottomLine.y = self.height - 1;
    if (self.bottomLineColor) {
        bottomLine.backgroundColor = self.bottomLineColor;
    } else {
        bottomLine.backgroundColor = CYNaviThemeColor;
    }
    [self addSubview:bottomLine];
}

/**
 *  导航条动画效果
 *
 *  @param lines
 *  @param to
 */
- (void)segmentBottomLines:(NSArray *)lines to:(NSInteger)to {
    UIView *line = lines.firstObject;
    if (!self.segmentBottomLineX) {
        self.segmentBottomLineX = line.x;
    }
    NSInteger index = to - self.fromIndex;
    //_minusLineW为减去的宽度
    self.segmentBottomLineX += index * (line.width + _minusLineW);
    //动画效果
    [UIView animateWithDuration:.5f delay:0 usingSpringWithDamping:.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        for (UIView *line in lines) {
            line.x = self.segmentBottomLineX;
        }
    } completion:nil];
    self.fromIndex = to;
}
@end
